var config = require('../../config')
Page({

  /**
   * 页面的初始数据
   */
  data: {

    sch_set: true,
    schArr: [],
    arrIndex: 0,
    provincial: null,
    provinceItem: [],
    proIndex: 0,
    city: null,
    cityItem: [],
    cityIndex: 0,
    sch_chs: null,
    sch_chs_city: null,
    sch_chs_pro: null,

    indicatorDots: true,
    autoplay: false,
    interval: 3000,
    duration: 1000,
    actList: [],
    objectArray: [],
    school: null,
    index: 0,

    mostRead:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    getApp().checkLogin(function () {
      wx.request({
        url: config.host + '/recommendAct'+'/mostRead',
        data: { sch_id: getApp().globalData.userInfo.sch_id },
        success:function(res){
          console.log(res);
          that.setData({mostRead:res.data});
          console.log(that.data.mostRead);
        }
      })
      wx.request({
        url: config.host + '/recommendAct',
        data: { open_id: getApp().globalData.userInfo.open_id },
        success: function (res) {
          console.log(res);
          that.setData({ actList: res.data })
        }
      });
      that.setData({ provinceItem: getApp().globalData.province });
      i = 0;
      while (getApp().globalData.userInfo.province != that.data.provinceItem[i]) {
        i++;
      }
      that.setData({
        sch_chs_pro: that.data.provinceItem[i],
        proIndex: i
      })
      getApp().reqCity({
        data: {
          province: that.data.provinceItem[that.data.proIndex]
        },
        success: function (res) {
          that.setData({ cityItem: res.data });
          i = 0;
          while (getApp().globalData.userInfo.city != that.data.cityItem[i]) {
            i++;
          }
          that.setData({
            sch_chs_city: that.data.cityItem[i],
            cityIndex: i
          })
          var th = that;
          getApp().reqSch({
            data: {
              province: th.data.sch_chs_pro,
              city: th.data.sch_chs_city
            },
            success: function (ret) {
              th.setData({ schArr: ret.data });
              i = 0;
              while (getApp().globalData.userInfo.city != th.data.cityItem[i]) {
                i++;
              }
              th.setData({
                sch_chs: th.data.schArr[i],
                school: th.data.schArr[i],
                arrIndex: i
              })
            }
          });
        },
        fail: function (res) {
          wx.showToast({
            title: '获取市列表失败',
            duration: 3000
          })
        }
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  todetail: function (event) {
    var articleId = event.currentTarget.dataset.articleId
    wx.setStorage({
      key: 'activity_info',
      data: this.data.actList[articleId],
    })
    wx.navigateTo({
      url: "../article/article",
    })
  },
  todetail_MostRead:function(event){
    var articleId = event.currentTarget.dataset.articleId
    wx.setStorage({
      key: 'activity_info',
      data: this.data.mostRead[articleId],
    })
    wx.navigateTo({
      url: "../article/article",
    })
  },
  unCollect: function (result) {
    var that = this;
    var event=result;
    var chgLike = that.data.actList;
    wx.request({
      url: config.host + '/actFollowing',
      method: 'DELETE',
      data: {
        open_id: getApp().globalData.userInfo.open_id,
        activity_id: result.currentTarget.id,
      },
      success:function(res){
        chgLike[event.currentTarget.dataset.id].like=false;
        that.setData({
          actList: chgLike
        })
      }
    });
    
  },

  collect:function(result){
    var that=this;
    var event=result;
    var chgLike = that.data.actList;
    wx.request({
      url: config.host+'/actFollowing',
      method:'POST',
      data: {
        open_id: getApp().globalData.userInfo.open_id,
        activity_id: result.currentTarget.id,
      },
      success:function(res){
        chgLike[event.currentTarget.dataset.id].like = true;
        that.setData({
          actList: chgLike
        })
      }
    })
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  chsProvince: function (e) {
    if (e.detail.value == this.data.proIndex) {
      return;
    }
    var that = this;
    this.setData({
      proIndex: e.detail.value,
    });
    this.data.sch_chs_pro = this.data.provinceItem[this.data.proIndex]
    this.setData({
      provincial: this.data.sch_chs_pro,
    });
    //console.log(this.data.sch_chs_pro);
    getApp().reqCity({
      data: {
        province: that.data.provincial
      },
      success: function (res) {
        console.log(res);
        that.setData({ cityItem: res.data });
        that.data.city = that.data.cityItem[0];
        var th = that;
        getApp().reqSch({
          data: {
            province: th.data.provincial,
            city: th.data.city
          },
          success: function (ret) {
            th.setData({ schArr: ret.data });
            th.data.school = th.data.schArr[0];
          }
        });
      },
      fail: function (res) {
        wx.showToast({
          title: '获取市列表失败',
          duration: 3000
        })
      }
    });
  },
  chsCity: function (e) {
    if (e.detail.value == this.data.cityIndex) {
      return;
    }
    var that = this;
    this.setData({
      cityIndex: e.detail.value,
    });
    this.data.sch_chs_city = this.data.cityItem[this.data.cityIndex];
    this.setData({
      city: this.data.sch_chs_city,
    });
    getApp().reqSch({
      data: {
        province: that.data.provincial,
        city: that.data.city
      },
      success: function (ret) {
        console.log(ret);
        that.setData({ schArr: ret.data });
        that.data.school = that.data.schArr[0];
      },
      fail: function (res) {
        wx.showToast({
          title: '获取学校列表失败',
          duration: 3000
        })
      }
    });
  },
  chsSchool: function (e) {
    if (e.detail.value == this.data.arrIndex) {
      return;
    }
    this.setData({
      arrIndex: e.detail.value,
    });
    this.data.sch_chs = this.data.schArr[this.data.arrIndex];
  },
  chgSch: function (res) {
    var that = this;
    console.log(this.data.sch_chs);
    if (this.data.sch_chs != this.data.school) {
      this.setData({ school: this.data.sch_chs });
      wx.request({
        url: config.host + '/recommendAct/chgSchAct',
        data: {
          province: that.data.sch_chs_pro,
          city: that.data.sch_chs_city,
          sch_name: that.data.sch_chs
        },
        success:function(res){
          console.log(res);
          that.setData({ actList: res.data });
        }
      });
    }
    this.closeModel();
  },
  closeModel: function () {
    this.setData({
      sch_set: !this.data.sch_set
    });
  },
  showactivity: function (event) {
    var key = event.detail.value;
    var that = this;
    wx.request({
      url: config.host + '/search',
      data: {
        key: key,
        open_id: getApp().globalData.userInfo.open_id
      },
      success: function (res) {
        that.setData({
          actList: res.data
        })
      }
    });
  }
})